package com.llgz.hotelmanageapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.llgz.hotelmanageapi.mapper.CheckinInfoMapper;
import com.llgz.hotelmanageapi.mapper.OrderInfoMapper;
import com.llgz.hotelmanageapi.mapper.OrderPersonMapper;
import com.llgz.hotelmanageapi.pojo.DAO.CheckinInfoDAO;
import com.llgz.hotelmanageapi.pojo.DO.CheckinInfo;
import com.llgz.hotelmanageapi.pojo.DO.OrderInfo;
import com.llgz.hotelmanageapi.pojo.DO.OrderPerson;
import com.llgz.hotelmanageapi.pojo.DO.RoomChangeInfo;
import com.llgz.hotelmanageapi.mapper.RoomChangeInfoMapper;
import com.llgz.hotelmanageapi.pojo.VO.CheckinInfoVO;
import com.llgz.hotelmanageapi.service.RoomChangeInfoServer;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 换房登记信息表 服务实现类
 * </p>
 *
 * @author Bernie_fang
 * @since 2021-07-19
 */
@Service
public class RoomChangeInfoServiceImpl extends ServiceImpl<RoomChangeInfoMapper, RoomChangeInfo> implements RoomChangeInfoServer {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private CheckinInfoMapper checkinInfoMapper;

    @Autowired
    private OrderPersonMapper orderPersonMapper;

    @Autowired
    private OrderPersonServiceImpl orderPersonService;

    @Autowired
    private RoomChangeInfoMapper roomChangeInfoMapper;

    @Autowired
    private RoomInfoServiceImpl roomInfoService;

    @Override
    public CheckinInfoVO getExChangRoomInfo(String id) {
        CheckinInfoVO checkinInfoVO = new CheckinInfoVO();
        OrderInfo orderInfo = orderInfoMapper.selectById(id);
        CheckinInfo checkinInfo = checkinInfoMapper.selectOne(new QueryWrapper<CheckinInfo>().eq("order_id", id));
        List<OrderPerson> orderPersonList = orderPersonMapper.selectList(new QueryWrapper<OrderPerson>().eq("order_id", id));
        BeanUtils.copyProperties(orderInfo,checkinInfoVO);
        checkinInfoVO.setOriginalRoomId(orderInfo.getRoomId());
        checkinInfoVO.setOriginalRoomType(orderInfo.getRoomType());
        // 入住人信息
        checkinInfoVO.setPeopleList(orderPersonList);
        checkinInfoVO.setBreakfastServed(checkinInfo.getBreakfastServed());
//        System.out.println("checkinInfoVO ==> " + checkinInfoVO);
        return checkinInfoVO;
    }

    @Override
    public void ChangeRoomInfo(CheckinInfoVO checkinInfoVO) {
//        System.out.println("getOriginalRoomId==> " + checkinInfoVO.getOriginalRoomId());
//        System.out.println("getRoomId==> " + checkinInfoVO.getRoomId());
//        System.out.println(checkinInfoVO.getRoomId().equals(checkinInfoVO.getOriginalRoomId()));
        // 如果是进行换房操作，需要记录换房信息
        if(!checkinInfoVO.getRoomId().equals(checkinInfoVO.getOriginalRoomId())){
            RoomChangeInfo roomChangeInfo = new RoomChangeInfo();
            OrderInfo orderInfo = new OrderInfo();
            // 获取入住单号
            CheckinInfo checkinId = checkinInfoMapper.selectOne(new QueryWrapper<CheckinInfo>().select("id").eq("order_id", checkinInfoVO.getId()));
            roomChangeInfo.setCheckinId(checkinId.getId());
            roomChangeInfo.setOrderId(checkinId.getId());
            roomChangeInfo.setOriginalRoomId(checkinInfoVO.getOriginalRoomId());
            roomChangeInfo.setChangeRoomId(checkinInfoVO.getRoomId());
            List<OrderPerson> peopleList = checkinInfoVO.getPeopleList();
            roomChangeInfo.setResidents(peopleList.get(0).getName());
            roomChangeInfo.setBreakfastServed(checkinInfoVO.getBreakfastServed());
            roomChangeInfo.setResidents(checkinInfoVO.getRemarks());
            roomChangeInfoMapper.insert(roomChangeInfo);
//            System.out.println("换房=>roomChangeInfo ==> " + roomChangeInfo);
            BeanUtils.copyProperties(checkinInfoVO,orderInfo);
            orderInfoMapper.updateById(orderInfo);
//            System.out.println("换房=>orderInfo ==> " + orderInfo);
            // 修改换房前后房间的状态（0，空房；1，已预定；2，入住）
            roomInfoService.updateRoomStatusByRoomId(checkinInfoVO.getRoomId(),2);
            roomInfoService.updateRoomStatusByRoomId(checkinInfoVO.getOriginalRoomId(),0);
        }else { // 不是换房，更新记录即可
//            System.out.println("#======== 更新记录");
            OrderInfo orderInfo = new OrderInfo();
            CheckinInfo checkinInfo = new CheckinInfo();
//            OrderPerson orderPerson = new OrderPerson();
            BeanUtils.copyProperties(checkinInfoVO,orderInfo);
            orderInfoMapper.updateById(orderInfo);
//            System.out.println("更新记录=>orderInfo ==> " + orderInfo);
            checkinInfo.setOrderId(checkinInfoVO.getId());
            checkinInfo.setBreakfastServed(checkinInfoVO.getBreakfastServed());
            CheckinInfo checkinId = checkinInfoMapper.selectOne(new QueryWrapper<CheckinInfo>().select("id").eq("order_id", checkinInfoVO.getId()));
            checkinInfo.setId(checkinId.getId());
            checkinInfoMapper.updateById(checkinInfo);
//            System.out.println("更新记录=>checkinInfo ==> " + checkinInfo);
            // 获取需更新的入住人信息
            List<OrderPerson> peopleList = checkinInfoVO.getPeopleList();
            orderPersonService.updateBatchById(peopleList);
//            System.out.println("更新记录=>OrderPerson=>peopleList ==> " + peopleList);
        }

    }

    @Override
    public void checkInWithRoomChange(CheckinInfoDAO checkinInfoDAO) {
        OrderInfo orderInfo = new OrderInfo();
        CheckinInfo checkinInfo = new CheckinInfo();
        BeanUtils.copyProperties(checkinInfoDAO,orderInfo);
        orderInfo.setOrderStatus(1);
        System.out.println("插入的订单信息 ==> " + orderInfo);
        Integer roomId = checkinInfoDAO.getRoomId();
        // 生成新的订单,需要获取新生成的order_id
        orderInfoMapper.insert(orderInfo);
        OrderInfo orderId = orderInfoMapper.selectOne(new QueryWrapper<OrderInfo>().select("id").eq("room_id", roomId));
        checkinInfoDAO.setOrderId(orderId.getId());
        System.out.println("新生成的order_id ==> " + orderId.getId());
        // 生成入住订单
        BeanUtils.copyProperties(checkinInfoDAO,checkinInfo);
        System.out.println("checkinInfo == >" + checkinInfo);
        checkinInfoMapper.insert(checkinInfo);
        // 插入入住人信息
        List<OrderPerson> peopleList = checkinInfoDAO.getPeopleList();
        for (OrderPerson orderPerson : peopleList) {
            orderPerson.setOrderId(orderId.getId());
        }
        orderPersonService.saveBatch(peopleList);
        System.out.println("入住人信息 ==> " + peopleList);
    }


}
